﻿Imports SFDL.PluginInterfaces
Imports System.ComponentModel.Composition

<Export(GetType(PluginInterfaces.IPostUnRARPlugin))>
Public Class RenamePlugin
    Implements IPostUnRARPlugin

    Dim _log As NLog.Logger = NLog.LogManager.GetLogger("RenamePlugin")

    Public Sub Initialize() Implements ISFDLLoaderPlugin.Initialize
        'Nothing to do here...

    End Sub

    Public Sub PostUnRAR(_archive_file_list As List(Of String), _volume_files As List(Of String), _extract_dir As String, _unrar_sucessful As Boolean) Implements IPostUnRARPlugin.PostUnRAR

        Dim _tvdb As New TheTVDB

        For Each _file In _archive_file_list

            Dim _test As String
            Dim _new_file As String
            Dim _searchstring As String
            Dim _searchresult As New SeriesParseResult

            Try

                _log.Info("Versuche Datei {0} zu parsen und entprechend umzubennen...", _file)

                _searchstring = IO.Path.GetFileName(_file)

                _searchresult = ParseSeriesFile(_searchstring, IO.Path.GetFileName(IO.Path.GetDirectoryName(_file)))

                'ToDo: Evtl. String splitten und danach mehere suchen durchführen

                _test = _tvdb.SearchSeries(_searchresult.QueryString, _searchresult.SeasonNumber, _searchresult.EpisodeNumber)

                If Not String.IsNullOrWhiteSpace(_test) Then

                    _new_file = _test & IO.Path.GetExtension(_file)
                    _new_file = IO.Path.Combine(IO.Path.GetDirectoryName(_file), _new_file)

                    _log.Info("Benne Datei {0} in {1} um!", _file, _new_file)

                    IO.File.Move(_file, _new_file)

                    _log.Info("Datei erfolgreich umbenannt!")

                End If

            Catch ex As Exception
                _log.Error(ex, ex.Message)
            End Try

        Next



    End Sub

    Public Sub SaveSettings() Implements ISFDLLoaderPlugin.SaveSettings
        Throw New NotImplementedException()
    End Sub

    Public Function GetAuthor() As String Implements ISFDLLoaderPlugin.GetAuthor
        Return "n0ix"
    End Function

    Public Function GetName() As String Implements ISFDLLoaderPlugin.GetName
        Return "Rename Plugin"
    End Function

    Public Function GetSettingsTab() As Windows.Forms.UserControl Implements ISFDLLoaderPlugin.GetSettingsTab
        Throw New NotImplementedException()
    End Function

    Public Function GetSettingsTabName() As String Implements ISFDLLoaderPlugin.GetSettingsTabName
        Throw New NotImplementedException()
    End Function

    Public Function GetVersion() As String Implements ISFDLLoaderPlugin.GetVersion
        Return "0.1 Alpha"
    End Function

    Public Function HasSettings() As Boolean Implements ISFDLLoaderPlugin.HasSettings
        Return False
    End Function
End Class
